import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager

def get_job_links(url):
    chrome_options = Options()
    # 先不开启无头模式方便调试
    # chrome_options.add_argument("--headless")
    chrome_options.add_argument("--disable-gpu")
    chrome_options.add_argument("--window-size=1920,1080")
    chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")

    service = Service(ChromeDriverManager().install())
    driver = webdriver.Chrome(service=service, options=chrome_options)

    job_links = []

    try:
        driver.get(url)
        wait = WebDriverWait(driver, 15)

        time.sleep(5)  # 加强等待，确保js加载完成

        all_links = driver.find_elements(By.TAG_NAME, "a")
        for link in all_links:
            href = link.get_attribute("href")
            if href and "jobdetail" in href:
                text = link.text.strip()
                if text:
                    job_links.append((text, href))
    except Exception as e:
        print("异常:", e)
    finally:
        driver.quit()

    return job_links


if __name__ == "__main__":
    url = 'https://www.zhaopin.com/sou/jl530/kw01L00O80EO062/p1'
    print(f"加载页面: {url}")
    jobs = get_job_links(url)
    print(f"找到 {len(jobs)} 个职位链接:")
    for title, link in jobs:
        print(f"{title} -> {link}")
